package com.imay.videorecord.camera.utils;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.util.Log;

import com.imay.videorecord.utils.ImayCons;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by why8222 on 2016/2/25.
 */
public class CameraUtils {

    public static Camera.Size getLargePictureSize(Camera camera){
        if(camera != null){
            List<Camera.Size> sizes = camera.getParameters().getSupportedPictureSizes();
            Camera.Size temp = sizes.get(0);
            for(int i = 1;i < sizes.size();i ++){
                float scale = (float)(sizes.get(i).height) / sizes.get(i).width;
                if(temp.width < sizes.get(i).width && scale < 0.6f && scale > 0.5f)
                    temp = sizes.get(i);
            }
            return temp;
        }
        return null;
    }

    public static Camera.Size getLargePreviewSize(Camera camera){
        if(camera != null){
            List<Camera.Size> sizes = camera.getParameters().getSupportedPreviewSizes();
            Camera.Size temp = sizes.get(0);
            for(int i = 1;i < sizes.size();i ++){
                if(temp.width < sizes.get(i).width)
                    temp = sizes.get(i);
            }
            return temp;
        }
        return null;
    }

    /**
     * 获得最接近频幕宽度的尺寸
     * @param sizeList
     * @param width       放大几倍 （>0)
     * @return
     */
    public static Camera.Size getCurrentScreenSize(List<Camera.Size> sizeList, int width,int height,double s) {
        if (sizeList != null && sizeList.size() > 0) {
            int screenwidth = 720;
            int screenheight = 1280;
            // 三星分辨率太高报错
            double c = ((double) ImayCons.screenH)/((double) ImayCons.screenW);
            Log.e("CameraUtils",ImayCons.screenW+"----"+ImayCons.screenH+"----"+screenwidth+"----"+c);
            int[] arry = new int[sizeList.size()];
            int temp = 0;
            for (Camera.Size size : sizeList) {
                double k = ((double) size.width)/((double) size.height);
                Log.v("CameraUtils",size.width+"----"+size.height+"----"+k);
                //int c = Math.max(size.width,size.height);
                arry[temp++] = Math.abs(size.height - screenwidth);
                if(size.width == screenheight && size.height == screenwidth){
                    return size;
                }
            }
            temp = 0;
            int index = 0;
            for (int i = 0; i < arry.length; i++) {
                if (i == 0) {
                    temp = arry[i];
                    index = 0;
                } else {
                    if (arry[i] < temp) {
                        index = i;
                        temp = arry[i];
                    }
                }
            }
            return sizeList.get(index);
        }
        return null;
    }


    /**
     * Attempts to find a preview size that matches the provided width and height (which
     * specify the dimensions of the encoded video).  If it fails to find a match it just
     * uses the default preview size for video.
     * <p>
     * TODO: should do a best-fit match, e.g.
     * https://github.com/commonsguy/cwac-camera/blob/master/camera/src/com/commonsware/cwac/camera/CameraUtils.java
     */
    public static void choosePreviewSize(Camera.Parameters parms) {
        // We should make sure that the requested MPEG size is less than the preferred
        // size, and has the same aspect ratio.
        Camera.Size ppsfv = parms.getPreferredPreviewSizeForVideo();
        if (ppsfv != null) {

        }

        if(currentScreenSize != null){
            Log.v(CameraUtils.class.getSimpleName(),currentScreenSize.width+"--1--"+currentScreenSize.height);
            parms.setPreviewSize(currentScreenSize.width, currentScreenSize.height);
            parms.setPreviewFormat(ImageFormat.YV12);
            return;
        }

        if (ppsfv != null) {
            parms.setPreviewSize(ppsfv.width, ppsfv.height);
        }
        // else use whatever the default size is
    }

    /**
     * Attempts to find a fixed preview frame rate that matches the desired frame rate.
     * <p>
     * It doesn't seem like there's a great deal of flexibility here.
     * <p>
     * TODO: follow the recipe from http://stackoverflow.com/questions/22639336/#22645327
     *
     * @return The expected frame rate, in thousands of frames per second.
     */
    public static int chooseFixedPreviewFps(Camera.Parameters parms, int desiredThousandFps) {
        List<int[]> supported = parms.getSupportedPreviewFpsRange();

        for (int[] entry : supported) {
            //Log.d(TAG, "entry: " + entry[0] + " - " + entry[1]);
            if ((entry[0] == entry[1]) && (entry[0] == desiredThousandFps)) {
                parms.setPreviewFpsRange(entry[0], entry[1]);
                return entry[0];
            }
        }

        int[] tmp = new int[2];
        parms.getPreviewFpsRange(tmp);
        int guess;
        if (tmp[0] == tmp[1]) {
            guess = tmp[0];
        } else {
            guess = tmp[1] / 2;     // shrug
        }
        return guess;
    }

    public static Camera.Size currentScreenSize;

    /**
     * 獲取前後攝像頭最接近屏幕相同的尺寸
     * @param width      放大几倍 （>0)
     * @return
     */
    public static Camera.Size getCurrentScreenSize(int width,int height) {
        if(currentScreenSize != null && currentScreenSize.width != 0 && currentScreenSize.height != 0){
            return currentScreenSize;
        }
        /**
         * 初始化最小的Size
         */
        List<Camera.Size> sizeList = new ArrayList<>();
        try{
            Camera camera = Camera.open(0);
            Camera.Parameters parameters = camera.getParameters();
            sizeList = parameters.getSupportedPreviewSizes();
            camera.release();
        }catch (Exception e){

        }

        List<Camera.Size> sizeList2 = new ArrayList<>();
        try{
            Camera camera2 = Camera.open(1);
            Camera.Parameters parameters2 = camera2.getParameters();
            sizeList2 = parameters2.getSupportedPreviewSizes();
            camera2.release();
        }catch (Exception e){

        }

        // PreviewSizes
        List<Camera.Size> sizeTarget = new ArrayList<>();
        for(int i = 0;i<sizeList.size();i++){
            final Camera.Size sizeI = sizeList.get(i);
            final int widthI = sizeI.width;
            final int heightI = sizeI.height;
            for(int j = 0;j<sizeList2.size();j++){
                final Camera.Size sizeJ = sizeList2.get(j);
                final int widthJ = sizeJ.width;
                final int heightJ = sizeJ.height;
                if(widthI == widthJ && heightI == heightJ){
                    sizeTarget.add(sizeI);
                    break;
                }else{
                    continue;
                }
            }
        }

        currentScreenSize = getCurrentScreenSize(sizeTarget, ImayCons.screenW,ImayCons.screenH,2);
        return currentScreenSize;
    }
}
